package com.aboo.design.singleton;

import org.junit.jupiter.api.Test;

import java.util.Random;

/**
 * 单例模式
 * 确保一个类最多只有一个实例，并提供一个全局访问点
 *
 * @author louan
 * @version 1.0
 * @date 2022/9/14
 */
public class SingletonPattern {

    @Test
    public void test01() {
        System.out.println(SingletonColor.getInstance().color());
        System.out.println(SingletonColor.getInstance().color());
        System.out.println(SingletonColor.getInstance().color());
    }
}

class SingletonColor {

    private static SingletonColor instance;

    private SingletonColor() {
    }

    public static synchronized SingletonColor getInstance() {
        if (instance == null) {
            instance = new SingletonColor();
        }
        return instance;
    }

    public String color() {
        return String.valueOf(100 + new Random().nextInt(100));
    }

}
